package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BasePageData;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.Category;
import com.cskaoyan.bean.admin.vo.CategoryInfoVO;
import com.cskaoyan.service.CategoryService;
import com.cskaoyan.utils.Constant;
import com.cskaoyan.utils.ErrState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Jcy
 * 商品类目
 */
@RestController
@RequestMapping("admin/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @RequestMapping("list")
    public BaseRespVo list() {
        BasePageData data = categoryService.list();
        return BaseRespVo.ok(data);
    }

    @RequestMapping("l1")
    public BaseRespVo l1() {
        BasePageData data = categoryService.l1();
        return BaseRespVo.ok(data);
    }

    @RequestMapping("create")
    public BaseRespVo create(@RequestBody Category category) {
        int code = categoryService.create(category);
        if (code == ErrState.LEVEL_ERROR.getErrno()) {
            return BaseRespVo.fail(ErrState.LEVEL_ERROR.getErrno(), ErrState.LEVEL_ERROR.getErrmsg());
        }
        if (code == Constant.FAIL) {
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody CategoryInfoVO categoryInfoVO){
        categoryService.delete(categoryInfoVO);
        return BaseRespVo.ok();
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody CategoryInfoVO categoryInfoVO){
        int code = categoryService.update(categoryInfoVO);
        if (code == Constant.FAIL) {
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok();
    }
}
